package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.human.EntityVisitorRecord;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.human.AddVisitorRecordVo;
import com.ocom.common.vo.human.GetVisitorRecordVo;
import com.ocom.human.dao.IDaoVisitorRecord;
import com.ocom.human.service.VisitorRecordService;
//import com.ocom.human.vo.AddVisitorRecordVo;
//import com.ocom.human.vo.GetVisitorRecordVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VisitorRecordServiceImpl extends ServiceImpl<IDaoVisitorRecord, EntityVisitorRecord> implements VisitorRecordService {


    @Autowired
    private IDaoVisitorRecord iDaoVisitorRecord;
    /**
     * 获取审批记录列表  (getVisitorRecord)
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityVisitorRecord> GetVisitorRecord(Long comId, GetVisitorRecordVo getVisitorRecordVo) {
        QueryWrapper<EntityVisitorRecord> query = Wrappers.query();
        query.eq("com_id",getVisitorRecordVo.getComId());

        if (StringUtils.isNotNull(getVisitorRecordVo.getId())){
            query.eq("id",getVisitorRecordVo.getId());
        }


        if (StringUtils.isNotEmpty(getVisitorRecordVo.getVisitorName())){
            query.eq("visitor_name",getVisitorRecordVo.getVisitorName());
        }
        if (StringUtils.isNotEmpty(getVisitorRecordVo.getApproverName())){
            query.eq("approver_name",getVisitorRecordVo.getApproverName());
        }
        if (StringUtils.isNotNull(getVisitorRecordVo.getApproverId())){
            query.like("approver_id",getVisitorRecordVo.getApproverId());
        }

        if (StringUtils.isNotEmpty(getVisitorRecordVo.getBeginTime())){
            query.ge("entry_time",getVisitorRecordVo.getBeginTime());
        }
        if (StringUtils.isNotEmpty(getVisitorRecordVo.getEndTime())){
            query.le("leave_time",getVisitorRecordVo.getEndTime());
        }

        query.ne("approver_status",3);

        query.last("order by approver_status asc,update_time desc");
        PageHelper.startPage(getVisitorRecordVo.getPage(),getVisitorRecordVo.getSize());

        return  iDaoVisitorRecord.selectList(query);
    }

    /**
     * 添加访客申请记录  (addVisitorRecord)
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityVisitorRecord AddVisitorRecord(Long comId, AddVisitorRecordVo addVisitorRecord) {
        EntityVisitorRecord entityVisitorRecord = new EntityVisitorRecord();
        if (StringUtils.isNotNull(addVisitorRecord.getComId())){
            entityVisitorRecord.setComId(addVisitorRecord.getComId());
        }
        if (StringUtils.isNotNull(addVisitorRecord.getDeptId())){
            entityVisitorRecord.setDeptId(addVisitorRecord.getDeptId());
        }
        if (StringUtils.isNotEmpty(addVisitorRecord.getDeptName())){
            entityVisitorRecord.setDeptName(addVisitorRecord.getDeptName());
        }
        if (StringUtils.isNotNull(addVisitorRecord.getApplyId())){
            entityVisitorRecord.setApplyId(addVisitorRecord.getApplyId());
        }

        if (StringUtils.isNotNull(addVisitorRecord.getApplyId())){
            entityVisitorRecord.setApplyId(addVisitorRecord.getApplyId());
        }

        if (StringUtils.isNotEmpty(addVisitorRecord.getVisitReason())){
            entityVisitorRecord.setVisitReason(addVisitorRecord.getVisitReason());
        }

        if (StringUtils.isNotEmpty(addVisitorRecord.getEntryTime())){
            entityVisitorRecord.setEntryTime(DateUtils.parseDate(addVisitorRecord.getEntryTime()));
        }
        entityVisitorRecord.setApproverStatus(0L);
        if (StringUtils.isNotEmpty(addVisitorRecord.getAreaIds())){
            entityVisitorRecord.setAreaIds(addVisitorRecord.getAreaIds());
        }
        if (StringUtils.isNotEmpty(addVisitorRecord.getAreaNames())){
            entityVisitorRecord.setAreaNames(addVisitorRecord.getAreaNames());
        }
        if (StringUtils.isNotNull(addVisitorRecord.getApproverId())){
            entityVisitorRecord.setApproverId(addVisitorRecord.getApproverId());
        }

        if (StringUtils.isNotEmpty(addVisitorRecord.getApproverName())){
            entityVisitorRecord.setApproverName(addVisitorRecord.getApproverName());
        }
        if (StringUtils.isNotEmpty(addVisitorRecord.getApplyName())){
            entityVisitorRecord.setApplyName(addVisitorRecord.getApplyName());
        }
        iDaoVisitorRecord.insert(entityVisitorRecord);
        return entityVisitorRecord;
    }
    /**
     * 修改访客申请记录  (updateVisitorRecord)
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityVisitorRecord UpdateVisitorRecord(Long comId, EntityVisitorRecord entityVisitorRecord) {
        iDaoVisitorRecord.updateById(entityVisitorRecord);
        return entityVisitorRecord;
    }

    // 删除访客申请记录（deleteVisitorRecord）
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DeleteVisitorRecord(Long comId, Long id) {
        iDaoVisitorRecord.deleteById(id);
    }


    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityVisitorRecord selectOne(Long comId, Long recodeId) {
         return iDaoVisitorRecord.selectById(recodeId);
    }
}
